/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.domain;

import commonlib.domain.ABPair;
import commonlib.domain.RuleGenerator;
import commonlib.domain.Rules;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import nominalitzacions.persistance.LanguageDataManagerController;


/**
 *
 * @author genis.sole
 */
public class LanguageDomainController {

    private Language lang;
    private LanguageDataManagerController LDMC;

    public LanguageDomainController() throws IOException {
        LDMC = new LanguageDataManagerController();
    }
    
    public void loadLanguage() throws IOException {
        lang = LDMC.loadLanguage();
        VerbalFormsController VFC = new VerbalFormsController();
        NominalizatorDictionary nd = VFC.createVerbalForms(lang.getInfDico(),
                        lang.getIrregDico() );
        lang.setInfDico(nd);
    }

    public Rules getRules() {
        return lang.rules;
    }

    public NominalizatorDictionary getNounDictionary() {
        return lang.nounDico;
    }

    public void generateRules(int n) {
        //System.out.println("Intentem generar les regles");
        Rules r = new Rules();
        //System.out.println("S'ha instanciat regles");
        RuleGenerator rGen = new RuleGenerator();
        //System.out.println("S'ha instanciat el generador de regles");
        Iterator<ABPair> it = lang.getCorpus().iterator();
        //System.out.println("S'ha instanciat l'iterador");
        while(it.hasNext()) {
            ABPair p = it.next();

            ArrayList<ABPair> rules = rGen.levNAssociation(p.getAlpha(),
                    p.getBeta(), n);
            //System.out.println(rules.size() );
            for(int i = 0; i < rules.size(); ++i) {
                r.addRule(rules.get(i), 1);
                //System.out.println(rules.get(i).toString());
            }
            //System.out.println(p.toString() );
        }
        //System.out.println(lang.rules.getRuleProb(new ABPair("re", "gnire") ) );
        //System.out.println(lang.rules.getRuleProb(new ABPair("gnire", "re") ) );
        lang.rules = r;
    }

    public boolean searchVerb(String verb) {
        verb = verb.toLowerCase();
        return lang.searchVerb(verb);
    }
    
    public void importLanguage(String infPath, String irregPath, String nounPath,
                            String corpusPath) throws IOException {
        LDMC.saveLanguage(infPath, irregPath, nounPath, corpusPath);
    }

    public void saveVerb(String verb) throws IOException {
        //lang.getInfDico().add(verb);
        LDMC.saveVerb(verb);
    }

    public void deleteVerb(String verb) throws IOException {
        //lang.getInfDico().deleteWord(verb);
        LDMC.deleteVerb(verb);
    }

    public void savePair(String alpha, String beta) throws IOException {
        //lang.getCorpus().AddPair(alpha, beta);
        LDMC.savePair(alpha, beta);
    }

    public void deletePair(String alpha, String beta) throws IOException {
        //lang.getCorpus().RemovePair(alpha, beta);
        LDMC.deletePair(alpha, beta);
    }
}
